var mysql = require('mysql'); //调用MySQL模块
//创建一个connection
var connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: '',
  database: 'test',
  port: '3306'
});

// 打开连接
connection.connect(function(err) {
  if(err) {
    console.log('[query] - : ' + err);
    return;
  }
  console.log('[connection connect] succeed!')
});

// 插入
/*
var userAddSql = 'insert into user(uname,pwd) values(?,?)';
var param = ['scott','tiger'];
connection.query(userAddSql, param, function(err,rs) {
  if(err) {
    console.log('insert err', err.message);
    return;
  }
  console.log('insert success');
});
*/

// 查询
connection.query('select * from user where uid = ?',[1], function(err, rows, fields) {
  if(err) {
    console.log('[query] - : ' + err);
    return;
  }
  for(var i = 0; i < rows.length; i++) {
    console.log('The solution is : ', rows[i].uname);
  }
  //console.log(fields);
});

//关闭连接
connection.end(function(err) {
  if(err) {
    console.log(err.toString());
    return;
  }
  console.log('[connection end] succeed!')
});
